//
//  TeleChargeRecordVC.swift
//  zhonglb_iOS
//
//  Created by lxm on 2021/12/8.
//

import UIKit

class TeleChargeRecordVC: ZMViewController, ListRequestObjct, UITableViewDelegate, UITableViewDataSource {
    //MARK:--------------------属性--------------------
    var listData = [TelePhoneModels.Record]()
    var page = 1
    var pageSize = 10
    var refreshScrollView : UIScrollView {
        tableView
    }
    //MARK: --------------------UI控件--------------------
    let tableView = UITableView().then { t in
        t.separatorStyle = .none
    }
    //MARK: --------------------生命周期--------------------
    override func viewDidLoad() {
        super.viewDidLoad()
        setUI()
        addConstraints()
        bindAction()
        tableView.mj_header?.beginRefreshing()
    }
    //MARK: --------------------UI--------------------
    func setUI() {
        title = "充值记录"
        view.addSubview(tableView)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = "#F5F5F5".zm.hex()
        tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        addRefresh(scrollView: tableView) { [weak self] refresh in
            guard let self = self else {return}
            if refresh {
                self.page = 1
            }
            self.loadList()
        }
        tableView.mj_header?.ignoredScrollViewContentInsetTop = 0
        tableView.zm.register(nibName: "TelePhoneRecodCell", reuserId: "TelePhoneRecodCell")
        addNodataScrollView(scrollView: tableView)
        emptyImg = "nodata"
    }
    func addConstraints() {
        tableView.style.snpEqualSuper()
    }
    
    //MARK: --------------------methods--------------------
    func bindAction() {
        
    }
    
    func loadList() {
        let pa = [
            "page" : page,
            "limit" : pageSize
        ] as [String : Any]
        
        ZMUtil.listRequest(type: TelePhoneModels.Record.self, api: TelephoneAPI.charge_record(params: pa)) { resp in
            self.listRequest(resp)
        }
    }

    //MARK: --------------------delegate--------------------
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        tableView.mj_footer?.isHidden = listData.count == 0
        return listData.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "TelePhoneRecodCell") as! TelePhoneRecodCell
        let data = listData[indexPath.row]
        cell.orderNo.text = "订单号：\(data.orderId)"
        cell.name.text = data.title //"手机话费充值 \(data.facePrice.zm.toString(keep: 2))元 \(data.typStr)"
        cell.phone.text = "手机号码：\(data.phone)"
        cell.money.text = data.payPrice.zm.toString(keep: 2)
        cell.refundBg.image = data.status == 2 ? UIImage.init(named: "home_back_u") : UIImage.init(named: "home_back")
        cell.refundMoney.text = "\(data.commission.zm.toString(keep: 2))元"
        cell.state.text = data.statusDes().title
        cell.state.textColor = data.statusDes().color
        if data.status == 2, data.refundStatus == 2 {
            // 失败, 已退款
            cell.refund.isHidden = false
        } else {
            cell.refund.isHidden = true
        }
       return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 200
    }
}
